<!-- 登录 -->
<template>
  <div class="login_container">
    <div class=""></div>
    <div class="login_box">
      <!-- 头像区域 -->
      <!-- <div class="avatar_box">
        <img @click="getCode" src="../../src/assets/03a290abfe5e6884640c68669b7d1c3a.jpg" alt="">
      </div> -->
      <span class="spanTitleStyle">登录{{ this.entryName }}</span>
      <SvgIcon1 name="404" />
      <SvgIcon1 name="bug" />
      <!-- 登录表单区域 -->
      <el-form
        :model="loginForm"
        ref="loginFormRef"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="loginName">
          <el-input
            v-model="loginForm.loginName"
            prefix-icon="iconfont icon-kehu-03-03"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            show-password
            placeholder="请输入密码"
          ></el-input>
          <!-- <el-input type="password" prefix-icon="el-icon-lock" v-model="loginForm.passWord" show-password></el-input>   显示隐藏密码 -->
        </el-form-item>

        <el-form-item prop="code">
          <div class="login-code">
            <el-input
              v-model="loginForm.captcha.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="login"
            >
              <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> -->
            </el-input>
            <!-- 远程二维码地址 -->
            <!-- <img src="https://336m330f01.goho.co/context/captcha/1" class="login-code-img" @click="getCode"> -->
            <!-- 本地二维码地址 -->
            <!-- <img src="http://192.168.3.5:8080/context/captcha/1" class="login-code-img" @click="getCode"> -->
            <!-- <span>{{ countdown }}</span> -->
            <img :src="imgCode" class="login-code-img" @click="getCode" />
            <!-- <img v-show="show" :src="imgCode" class="login-code-img" @click="getCode"> -->
          </div>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <span class="titleStyle"
      >版权所有：河南祺智科技有限公司 © {{ time }} - {{ timeAddOne }}</span
    >
  </div>
</template>

<script>
import SvgIcon1 from "./SvgIcon1";
import axios from "axios";
export default {
  data() {
    return {
      countdown: "5",
      time: "",
      timeAddOne: "",
      timer: null,
      isReloadData: true,
      entryName: "",
      codeUrl: "",
      zidian: [],
      imgCode: "http://192.168.3.180:8081/context/captcha/1", // 本地地址
      // imgCode: 'http://192.168.3.6:8080/context/captcha/1', // 测试地址
      // imgCode: 'https://3d36v33001.imdo.co/context/captcha/1', // 远程地址
      // 这是登录表单的数据绑定对象
      loginForm: {
        captcha: {
          code: "", //验证码
          serialNumber: "1", // 流水号
        },
        loginName: "",
        password: "",
        loginType: "PC", //登录设备
        currentSystemType: "BACKGROUND_SYSTEM",
        // 系统权重类型
      },
      // 登录表单验证规则
      // 表单校验规则
      loginFormRules: {
        // 验证用户名是否合法
        loginName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" },
        ],
      },
    };
  },
  components: { SvgIcon1 },
  created() {
    this.getCode();
    this.getEntryNameHandle();
  },
  methods: {
    // 重置按钮
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 缓存字典项
    async shuaXinHandler() {
      const { data: res } = await this.$http.get("/sys-dict-type/cacheDict");
      if (res.code === 20000) {
        sessionStorage.setItem("dictData", JSON.stringify(res.data));
      }
    },
    // 项目名称
    async getEntryNameHandle() {
      const { data: res } = await this.$http.get("/home/systemName");
      if (res.code === 20000) {
        this.entryName = res.data;
      }
    },
    // 登录按钮
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        // console.log(valid)
        if (!valid) return;
        const result = await this.$http.post("/context/login", this.loginForm);
        // const { data : result} = await this.$http.post('/xq-sys-user/login', this.loginForm);
        console.log(result.data.code);
        if (result.data.code === 20000) {
          console.log(result.data.data.tree);

          this.shuaXinHandler();
          this.$message({
            message: "登陆成功",
            type: "success",
            center: true,
          });
          // 存储token
          // window.sessionStorage.setItem('satoken', result.config.baseURL + result.headers + result.status);
          // window.sessionStorage.setItem('satoken', result.data.data.token);
          sessionStorage.setItem("userdata", JSON.stringify(result.data.data));
          sessionStorage.setItem("satoken", result.data.data.token);
          sessionStorage.setItem("userInfo", result.data.data.userName);
          sessionStorage.setItem("treeData", JSON.stringify(result.data.data.tree));
          sessionStorage.setItem("user", JSON.stringify(this.loginForm));
          // 跳转home主页
          this.$router.push("/home");
        } else {
          this.$message({
            message: result.data.msg,
            type: "error",
            center: true,
          });
        }
      });
    },
    // 图形验证码
    async getCode() {
      var date = new Date(); // 日期
      this.time = date.getFullYear(); //表示当前日期
      this.timeAddOne = date.getFullYear() + 1; //表示当前日期

      // this.$router.go(0)
      // var num = Math.ceil(Math.random() * 10);//生成一个随机数（防止缓存）

      // 协议
      let protocol = window.location.protocol; //http
      let host = window.location.host; // 192.168.3.5
      // this.imgCode = 'https://3d36v33001.imdo.co/context/captcha/1';
      // this.imgCode = `http://192.168.3.23:8080/context/captcha/1?${new Date().getTime()}` // cjx 服务
      // this.imgCode = `http://192.168.3.48:8080/context/captcha/1?${new Date().getTime()}` // djy 服务
      this.imgCode = `${
        axios.defaults.baseURL
      }/context/captcha/1?${new Date().getTime()}`; // 本地 打包地址
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: pink;
  height: 100%;
  background-image: url("../../src/assets/bgc.png");
  background-repeat: no-repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  background-position: center 0;
}

.login_box {
  width: 450px;
  height: 340px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.1);
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 70%;
  transform: translate(-50%, -50%);

  .spanTitleStyle {
    display: flex;
    justify-content: center;
    margin-top: 30px;
    font-size: 20px;
  }

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #fff;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #fff;
    }
  }
}

.login_form {
  position: absolute;
  top: 100px;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
  // margin-top: -40px;
}

.login-code {
  display: flex;
  justify-content: space-between;
}

.login-code-img {
  width: 100px;
  height: 38px;
}

.titleStyle {
  position: absolute;
  right: 40%;
  bottom: 0;
  margin-bottom: 20px;
  color: gray;
}
</style>
